from PyQt4.QtCore   import QObject, QTimer, pyqtSignal
    

class Timer(QObject):
    """ timer class 
    """

    __timers = []
    timeout = pyqtSignal() #: signal emited when timer reaches 0

    def __init__(self, parent=None):

        super(Timer, self).__init__(parent)

        self.timer = QTimer()
        self.timer.setSingleShot(True)
        self.__timers.append(self)

    def __del__(self):
        self.__timers.remove(self)

    def __onTimeout(self, cb=None):
        self.timeout.emit()
        if cb: cb()

    def start(self, duration, cb=None):
        """ :param int duration: duration in seconds
            :param cb: callback called wenn timer reaches 0
            :type cb: callable or None

            start timer, after *duration* seconds callback will be called
            and the timeout signal will be emited
        """
        self.timer.timeout.connect( lambda: self.__onTimeout(cb) )
        self.timer.start(duration*1000)

    def stop(self):
        """ stop timer """
        self.timer.stop()
        self.timer.timeout.disconnect()

    #def onTimeout(self):
        #pass


